package com.woniuxy.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.core.model.PageInfo;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.staticCode.OrderCode;
import com.woniuxy.order.dao.OrderInfo;
import com.woniuxy.order.dao.OrderOvertime;
import com.woniuxy.order.feign.StallInfoClient;
import com.woniuxy.order.feign.UserInfoClient;
import com.woniuxy.order.mapper.OrderOvertimeMapper;
import com.woniuxy.order.service.OrderInfoService;
import com.woniuxy.order.service.OrderOvertimeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.order.service.impl.dto.OrderOvertimeDTO;
import com.woniuxy.stall.dao.StallInfo;
import com.woniuxy.user.dao.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 凌玉春
 * @since 2024年01月15日
 */
@Service
public class OrderOvertimeServiceImpl extends ServiceImpl<OrderOvertimeMapper, OrderOvertime> implements OrderOvertimeService {
    @Autowired
    private OrderOvertimeMapper orderOvertimeMapper;
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private UserInfoClient userInfoClient;
    @Autowired
    private StallInfoClient stallInfoClient;
    @Override
    public void add(OrderOvertime orderOvertime) {
        int i = orderOvertimeMapper.insert(orderOvertime);
    }


    @Override
    public PageInfo platformGetOverTimeOrderPage(Page<OrderOvertime> page) {
        System.out.println("BBBBBBBBBBBBBBBBBBBBB");
        Page<OrderOvertime> orderOvertimePage = orderOvertimeMapper.selectPage(page, null);
        List<OrderOvertime> records = orderOvertimePage.getRecords();
        System.out.println(records.size()+"mmmmmmmmm");
        ArrayList<OrderOvertimeDTO> dto=new ArrayList<OrderOvertimeDTO>();
        for (OrderOvertime orderOvertime : records) {
            OrderInfo orderInfo = orderInfoService.getById(orderOvertime.getOrderInfoId());
            Result<UserInfo> userInfoResult = userInfoClient.loadAllUserinfobyId(orderInfo.getUserInfoId());
            UserInfo userInfo = userInfoResult.getData();
            OrderOvertimeDTO orderOvertimeDTO = BeanUtil.toBean(orderOvertime, OrderOvertimeDTO.class);
            orderOvertimeDTO.setUserName(userInfo.getUserInfoName());
            Result<StallInfo> stallInfoResult = stallInfoClient.findStallById(orderInfo.getStatllInfoId());
            StallInfo stallInfo = stallInfoResult.getData();
            orderOvertimeDTO.setStallInfoNum(stallInfo.getStallInfoNum());
            Integer payState = orderOvertime.getPayState();
            String payStateName=payState== OrderCode.ORDER_OVER_TIME_NOT_PAY.getCode()?OrderCode.ORDER_OVER_TIME_NOT_PAY.getMessage():OrderCode.ORDER_OVER_TIME_PAYED.getMessage();
            orderOvertimeDTO.setPayStateName(payStateName);
            dto.add(orderOvertimeDTO);
        }
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setRecords(dto);
        return pageInfo;
    }

    @Override
    public OrderOvertime getByOrderInfoId(Integer orderInfoId) {
        QueryWrapper<OrderOvertime> wrapper = new QueryWrapper<>();
        wrapper.eq("order_info_id",orderInfoId);
        OrderOvertime orderOvertime = orderOvertimeMapper.selectOne(wrapper);
        return orderOvertime;
    }
}
